package org.jiuling.tencent.sms;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.jiuling.tencent.sms.config.TencentConfig;
import org.jiuling.tencent.sms.config.TencentSMSConfig;
import org.jiuling.tencent.sms.config.enums.RegionEnum;

/**
 * 腾讯短信
 * @author 紫灵
 * @date 2021年12月03日 23时26分32秒
 */
public class TencentSMS {

    private final TencentSMSConfig tencentSMSConfig;
    private final TencentConfig tencentConfig;

    public TencentSMS(TencentSMSConfig tencentSMSConfig) {
        this.tencentSMSConfig = tencentSMSConfig;
        this.tencentConfig = tencentSMSConfig.getTencentConfig();
    }

    /**
     * 发送消息
     * @param templateId 模板id
     * @param phoneNumberArray 电话列表
     * @param templateParamArray 模板参数列表
     */
    public void sendMess(String templateId, String[] phoneNumberArray,String[] templateParamArray) throws TencentCloudSDKException {
        Credential cred = new Credential(tencentConfig.getSecretId(), tencentConfig.getSecretKey());
        ClientProfile clientProfile = new ClientProfile();
        SmsClient client = new SmsClient(cred, tencentConfig.getRegion().getCode(),clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(tencentSMSConfig.getSdkAppId());
        req.setSignName(tencentSMSConfig.getSignName());
        req.setTemplateId(templateId);
        req.setPhoneNumberSet(phoneNumberArray);
        req.setTemplateParamSet(templateParamArray);
        SendSmsResponse res = client.SendSms(req);
        System.out.println(SendSmsResponse.toJsonString(res));
        System.out.println(res.getRequestId());
    }
}
